package com.yinda.dt.produce.app;

import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.mingle.widget.ShapeLoadingDialog;
import com.xcgame.oh.ResponseCallback;
import com.xcgame.oh.UploadResponseCallback;
import com.yinda.dt.produce.utils.ApplicationUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

import okhttp3.Call;

/**
 * 描述
 * <br/>
 * Created by 小灿 on 2017/2/23.
 */

public class DemoActivity extends AppCompatActivity{

    protected MyOkHttp myOkHttp;
    protected ShapeLoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myOkHttp = new MyOkHttp();
        loadingDialog = new ShapeLoadingDialog(this);
        loadingDialog.setLoadingText("加载中...");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        myOkHttp.cancelAll();
    }

    public Call post(String url, JSONObject jsonParams, ResponseCallback callback) {
        try {
            jsonParams.put("key", DemoConfig.getInstance(this).getKey());
            jsonParams.put("phoneModel", Build.BRAND + "-" + Build.MODEL + "-" + Build.VERSION.RELEASE);
            jsonParams.put("appVersion", ApplicationUtil.getThisAPPVersionName(this));
            jsonParams.put("phoneType", "android");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.i("网络请求params", jsonParams.toString());
        return myOkHttp.post(this, url, jsonParams, callback);
    }

    public void uploadFile(String url, JSONObject jsonParams, File file, UploadResponseCallback callback) {
        try {
            if (jsonParams == null) {
                jsonParams = new JSONObject();
            }
            jsonParams.put("key", DemoConfig.getInstance(this).getKey());
            jsonParams.put("phoneModel", Build.BRAND + "-" + Build.MODEL + "-" + Build.VERSION.RELEASE);
            jsonParams.put("appVersion", ApplicationUtil.getThisAPPVersionName(this));
            jsonParams.put("phoneType", "android");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.i("网络请求params", jsonParams.toString());
        myOkHttp.uploadFile(this, url, jsonParams, file, callback);
    }

    public DemoApplication getDemoApplication() {
        return (DemoApplication) getApplication();
    }

    public boolean isServiceWork(String serviceName) {
        return ((DemoApplication) getApplication()).isServiceWork(serviceName);
    }

    public void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }
}
